<?php
defined('IN_TS') or die('Access Denied.');
// 用户是否登录
$userid = aac('user') -> isLogin();

switch ($ts) {
	//添加课时
	case "":		
	
		$studyid = intval($_GET['id']); 
		$strStudy = $new['study']->find('study',array(
		
			'studyid'=>$studyid,
		
		));
		
		if($strStudy['userid'] == $userid || $TS_USER['isadmin']==1){
		
			//第几课时
			$lessonNum = $new['study']->findCount('study_lesson',array(
				'studyid'=>$strStudy['studyid'],
			));
			
			$orderid = $lessonNum+1;
			
			//课程视频
			$arrVideo = $new['study']->findAll('study_video',array(
				'studyid'=>$strStudy['studyid'],
			));


			$title = '发布课时'; 
			// 包含模版
			include template("add");
		
		}else{
		
			tsNotice('非法操作！');
		
		}
		
		break; 
		
	// 执行发布话题
	case "do":
		
        $studyid = intval($_POST['studyid']);
		
		$title = tsClean($_POST['title']);
		$content = tsClean($_POST['content']);
		$orderid = intval($_POST['orderid']);
		
		$videoid = intval($_POST['videoid']);
		
		if($title == '' || $content == '' || $orderid==0 || $studyid==0){
			tsNotice('课时发布输入不完整！');
		}
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));
	
		$lessonid = $new['study']->create('study_lesson',array(
			'studyid' => $studyid,
			'userid' => $userid,
			'orderid' => $orderid,
			'videoid' => $videoid,
			'title' => $title,
			'content' => $content,
			'addtime' => time(),
		));
		
		
		//统计总课时
		$count_lesson = $new['study']->findCount('study_lesson',array(
			'studyid'=>$studyid,
		));
		$new['study']->update('study',array(
			'studyid'=>$studyid,
		),array(
			'count_lesson'=>$count_lesson,
		));
		
		
		header("Location: " . tsUrl('study', 'lesson', array('id' => $lessonid)));
		break; 
		
	// 快速发帖
	
} 
